#!/usr/bin/env bash
#########################################################################
# File Name   : run-deploy.sh
# Author      : seadog
# mail        : seadog0514@163.com
# Version     : 0.1
# Created Time: Thu 2024-10-03 10:47:51(+0800)
#########################################################################

#
#
# Usage:
#   1. update inventory.yaml
#   2. prepare ssl certificates for control/neutron nodes

{
  set -euo pipefail
  export TZ="Asia/Shanghai"
  APP_ROOT=$(dirname "$(readlink -fm "$0")")
  cd "${APP_ROOT}"

  exec &> >(tee -a "/tmp/openstack-deploy.log")
}

# !!! deploy
echo "deploy common settings"
ansible-playbook -i inventory.yaml deploy-common-settings.yaml

echo "deploy control-node"
ansible-playbook -i inventory.yaml deploy-master.yaml

echo "deploy neutron"
ansible-playbook -i inventory.yaml deploy-neutron-node.yaml

echo "deploy clinder-storage"
ansible-playbook -i inventory.yaml deploy-cinder-storage.yaml

echo "deploy nova-compute"
ansible-playbook -i inventory.yaml deploy-nova-compute.yaml

# !!! check status
echo "after boot up check status"
scp -i ~/.files_saved/dell-249-private.txt ./scripts/master_status_check.sh 192.168.0.81:/root/
ssh -T 192.168.0.81 'bash /root/master_status_check.sh'

# !!! network/vm creation
scp -i ~/.files_saved/dell-249-private.txt ./files/cirros-0.6.3-x86_64-disk.img 192.168.0.81:/root/ &>/dev/null || true
# network/flavor/keypair/image
echo "network/flavor/keypair/image"
scp -i ~/.files_saved/dell-249-private.txt ./scripts/create_network.sh 192.168.0.81:/root/network.sh
ssh -i ~/.files_saved/dell-249-private.txt -T 192.168.0.81 'bash /root/network.sh'
# test vm
echo "create test vm"
scp -i ~/.files_saved/dell-249-private.txt ./scripts/create_vm_cirros.sh 192.168.0.81:/root/vm.sh
ssh -i ~/.files_saved/dell-249-private.txt -T 192.168.0.81 'bash /root/vm.sh'

# !!! check status
echo "final check status"
scp -i ~/.files_saved/dell-249-private.txt ./scripts/master_status_check.sh 192.168.0.81:/root/
ssh -T 192.168.0.81 'bash /root/master_status_check.sh'

# !!! basic info
# # check nova service
# openstack compute service list
#
# # list network agent
# openstack network agent list
#
# # cinder
# openstack volume service list
#
# # server
# openstack server list
#
# openstack hypervisor list
# openstack image list
#
# # horizone
# http://192.168.0.81/horizon
# user/pass: cat ~/keystonerc
